var globalGivenGames = {};
var globalViewer = {};
var globalFriends = {};

function postActivity(game_name, friend) {
  var title = globalViewer.getDisplayName() + ' has dedicated ' + game_name + ' to ' + globalFriends[friend];
  var params = {};
  params[opensocial.Activity.Field.TITLE] = title;
  var activity = opensocial.newActivity(params);
  opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function() {});
}

function updateReceivedList(viewer, data, friends) {
  var viewerId = viewer.getId();
  var counter = 0;
  var html = new Array();
  html.push('Your friends recommended the following games:<ul>');
  friends.each(function(person) {
    var personData = data[person.getId()];
    if (personData) {
      var json = data[person.getId()]['games'];
    
      var Games = {}
      if (!json) {
        Games = {};
      }
      try {
        Games = gadgets.json.parse(gadgets.util.unescapeString(json));
      } catch (e) {
        Games = {};
      }
    
      for (i in Games) {
        if (+(i) > 0 && i == viewerId) {
          counter++;
          html.push('<li><a href="javascript:void(0);" onclick="makeNormalRequest(\'' + Games[i] + '\')">' + Games[i] + '</a> from ' + person.getDisplayName() + '</li>');
        }
      }
    }
  });
  html.push('</ul>');
  if(counter > 0)
  	document.getElementById('received').innerHTML = html.join('');
  //test counter
  alert(counter);
}

function updateGameList(viewer, data, friends) {
  var counter = 0;
  var json = null;
  if(data[viewer.getId()])
	json = data[viewer.getId()]['games'];
  
  if (!json) {
    globalGivenGames = {};
  }
  try {
    globalGivenGames = gadgets.json.parse(gadgets.util.unescapeString(json));
  } catch (e) {
    globalGivenGames = {};
  }
  
  var html = new Array();
  html.push('Your recommendations:');
  html.push('<ul>');
  for (i in globalGivenGames) {
    if (+(i) > 0) {
      html.push('<li>' + friends[i] + ' received <a href="javascript:void(0);" onclick="makeNormalRequest(\'' + globalGivenGames[i] + '\')">' + globalGivenGames[i] + '</a></li>');
      counter++;
    }
  }
  html.push('</ul>');
  if(counter > 0)
	document.getElementById('given').innerHTML = html.join('');
}

function giveGame() {
  var friend = document.getElementById('person').value;
  globalGivenGames[friend] = document.getElementById('currentgame').innerHTML;
  var json = gadgets.json.stringify(globalGivenGames);
  
  var req = opensocial.newDataRequest();
  req.add(req.newUpdatePersonAppDataRequest(opensocial.DataRequest.PersonId.VIEWER, 'games', json));
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'games'), 'data');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'games'), 'viewerFriendData');
  req.send(onLoadFriends);
  
  postActivity(globalGivenGames[friend], friend);
}

function loadFriends() {
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
  req.add(req.newFetchPeopleRequest('VIEWER_FRIENDS'), 'viewerFriends');
  req.add(req.newFetchPersonAppDataRequest('VIEWER', 'games'), 'data');
  req.add(req.newFetchPersonAppDataRequest('VIEWER_FRIENDS', 'games'), 'viewerFriendData');
  req.send(onLoadFriends);
}

function onLoadFriends(data) {
  var viewer = globalViewer = data.get('viewer').getData();
  var viewerFriends = data.get('viewerFriends').getData();
  var GameData = data.get('data').getData();
  var viewerFriendData = data.get('viewerFriendData').getData();
  var friends = new Array();
  
  html = new Array();
  html.push('<select id="person">');
  viewerFriends.each(function(person) {
    html.push('<option value="' + person.getId() + '">' + person.getDisplayName() + "</option>");
    friends[person.getId()] = person.getDisplayName();
  });
  html.push('</select>');
  document.getElementById('friends').innerHTML = html.join('');
  
  globalFriends = friends;
  updateGameList(viewer, GameData, friends);
  updateReceivedList(viewer, viewerFriendData, viewerFriends);
}

function init() {
  loadFriends();
}


function getURLFromGameName(name)  {
  if (name == 'Mario')
  	return 'http://blinkyou.com/codes/flash/mario.swf';
  if (name == 'Tic-Tac-Toe')
  	return 'http://blinkyou.com/codes/flash/tictactoe.swf';
  if (name == 'Chess')
  	return 'http://www.needtoshout.com/video/nae/chess.swf';	  
  if (name == 'Copter')
  	return 'http://blinkyou.com/codes/flash/copter.swf';
  if (name == 'Duck Hunt')
  	return 'http://blinkyou.com/codes/flash/duckhunt.swf';	
  if (name == 'Simon')
  	return 'http://blinkyou.com/codes/flash/copter.swf';
  if (name == 'Sonic')
  	return 'http://blinkyou.com/codes/flash/simon.swf';	
  if (name == 'Asteroids')
  	return 'http://blinkyou.com/codes/flash/asteroids.swf';	

}

function selectionChanged()  {
  var index = document.getElementById('games_choice').selectedIndex;
  var gamename = document.getElementById('games_choice').options[index].value;

  var url = getURLFromGameName(gamename);
		    	
		      document.getElementById('content_div').innerHTML = 
		      "<a href=\"JavaScript:void(0);\" onclick=\"backToGames()\">Back to games</a><br/><embed src=\"" + url + "\" type=\"application/x-shockwave-flash\" height=\"350\" width=\"100%\"></embed>";   		      
		      document.getElementById('currentgame').innerHTML = name;
		      
				HideContent('games');
				ShowContent('gameview');
				ShowContent('content_div');

}